package com.fh.modules.pms.service.impl;

import com.fh.common.exception.Assert;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.pms.entity.PmsProduct;
import com.fh.modules.pms.mapper.PmsProductMapper;
import com.fh.modules.pms.model.mobile.ProductListModel;
import com.fh.modules.pms.service.IPmsProductCpsService;
import com.fh.modules.pms.service.IPmsProductService;
import com.fh.modules.ums.entity.UmsPartnerLevel;
import com.fh.modules.ums.service.IUmsPartnerLevelService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 商品销售提成
 * @author： code generator
 * @date：   2019-09-27
 * @version： V1.0
 */
@Service
public class PmsProductServiceImpl extends AbstractServiceImpl<PmsProductMapper, PmsProduct> implements IPmsProductService {

    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private IPmsProductCpsService pmsProductCpsService;
    @Autowired
    private IUmsPartnerLevelService umsPartnerLevelService;
    @Override
    public PmsProduct getOrderProduct(Serializable id) {
        PmsProduct pmsProduct=super.getById(id);
        Assert.isTrue(pmsProduct==null||pmsProduct.getDelFlag()==1,"下单商品已经被删除");
        Assert.isTrue(pmsProduct.getPublishStatus()==0,pmsProduct.getName()+"已经下架");
        Assert.isTrue(pmsProduct.getStock()==0,pmsProduct.getName()+"无库存");
        return pmsProduct;
    }

    @Override
    public void onOffProduct(Integer id,Integer publishStatus) {
        if(id!=null){
            PmsProduct pmsProduct= super.getById(id);
            if(pmsProduct!=null){
                pmsProduct.setPublishStatus(publishStatus);
                this.updateById(pmsProduct);
            }
        }
    }

    @Override
    public List<ProductListModel> queryProductPage(String keyword, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize,false);
        return pmsProductMapper.queryProductPage(keyword);
    }

    @Override
    public Integer getSalePercentage(Integer productId, Integer partnerLevelId) {
        Integer salePercentage=pmsProductCpsService.getSalePercentage(productId,partnerLevelId);
        return salePercentage==null?umsPartnerLevelService.getById(partnerLevelId).getPercentage():salePercentage;
    }

    @Override
    public void deleteEntityByDelFlag(PmsProduct entity) {
        PmsProduct pmsProduct=this.getById(entity.getId());
        Assert.isTrue(pmsProduct.getPublishStatus()==1,"上架商品不能删除");
        super.deleteEntityByDelFlag(entity);
    }
}
